package com.leyou.item.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dao.AclMapper;
import com.leyou.item.pojo.Acl;
import com.leyou.item.pojo.AclModule;
import com.leyou.item.pojo.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author:yanjizhw@163.com
 * @date:2019-10-14 8:07
 * 描述:
 * @since:1.0.0
 */
@Service
public class AclService {

    @Autowired
    private AclMapper aclMapper;

    public void save(Acl acl) {

        String code = new SimpleDateFormat().format(new Date()) + (int)Math.random() * 100;

        acl.setId(null);
        acl.setCode(code);
        acl.setOperator("admin");
        acl.setOperate_time(new Date());
        acl.setOperate_ip("127.0.0.1");

        aclMapper.insert(acl);
    }

    public PageInfo<Acl> queryAcl(Integer page, int pageSize) {

        //分页
        PageHelper.startPage(page, pageSize);

        List<Acl> acls = aclMapper.selectAclAndAclModule();

        if (CollectionUtils.isEmpty(acls)){
            throw new LyException(ExceptionEnum.ACL_NOT_FOUND);
        }

        PageInfo<Acl> pageInfo = new PageInfo<>(acls);

        return pageInfo;
    }

    public List<Acl> querySelectAclParent() {

        Example example = new Example(Acl.class);
        example.createCriteria().andEqualTo("type", 2);
        return aclMapper.selectByExample(example);
    }
}
